using NodeCanvas.Framework;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.StreetEvent;

namespace Yoozoo.Gameplay.Liberty.AI.Node
{
    public class NpcIsNearItem : AIConditionBaseNode
    {
        public float Distance = 10;
        public BBParameter<Vector3> TargetPosition;
        protected override bool OnCheck()
        {
            var list = StreetFightEvent.Instance.GetAllItemTrans();
            if (list == null)
            {
                return false;
            }
            Transform targetTrans = null;
            if (list != null)
            {
                var closestDistance = float.MaxValue;
                foreach (var item in list)
                {
                    //判断是否能拾取
                    if (item.GetItemCanPick(_entity.data.side))
                    {
                        var distance = Vector3.Distance(item.transform.position, _entity.data.position);
                        if (distance < closestDistance && distance < Distance)
                        {
                            closestDistance = distance;
                            targetTrans = item.transform;
                        }
                    }
                }

                if (targetTrans!=null)
                {
                    TargetPosition.value = targetTrans.position;
                    return true;
                }
            }

            return false;
        }
    }
}